//# sourceURL=http://uMapApp/Module/RoadMaterials/RoadMaterials
define([
    "mhjs/mhjs"
    , "uMap/uMap"
    , "uEarth/uEarth"
    , "../../UI/Layer"
    , "../../Earth/ViewPort"
    , "../StakeNum/StakeNum"
], function (
    mhjs,
    uMap,
    uEarth,
    Layer,
    ViewPort,
    StakeNum
) {

    function RoadMaterials(option) {
        var _this = this;

        _this.colors = {
            normal: ["#ff0", 0.01],
            hover : ["#ff0", 0.25],
            active: ["#ff0", 0.5]
        };


        option && _this.OptionToProperties(option);

        _this.stakeNum = StakeNum.transform(_this["桩号"]);

        _this.title = _this["桩号"];
        _this.subtitle = _this["桩号"];

        _this.searchKey = _this["桩号"];
        _this.sortKey = _this.stakeNum.distance;

        if (_this.positions) {

            _this.positions = new uMap.Positions(_this.positions);

            _this.layer = new Layer({
                "app"     : _this.app,
                "viewport": new ViewPort({
                    "center": _this.positions.positions[2],
                    "hpr"   : {
                        "heading": uMap.Positions.getHeading(_this.positions.positions[0], _this.positions.positions[2]),
                        "pitch"  : -20,
                        "range"  : 600
                    }
                })
            });

            _this.entity = new uMap.Entity({
                objectOption: {
                    polyline: new uMap.PolylineGraphic({
                        objectOption: {
                            positions    : _this.positions.positions.concat([_this.positions.positions[0]]),
                            clampToGround: true,
                            width        : 2,
                            color        : ["#fff", 0.5]
                        }
                    }),
                    polygon : new uMap.PolygonGraphic({
                        objectOption: {
                            positions: {positions: _this.positions},
                            color    : _this.colors.normal
                            // , outlineColor: ["#ff0"]
                            // , extrudedHeight: 1
                            // , extrudedHeightReference: uEarth.HeightReference.RELATIVE_TO_GROUND
                        }
                    })
                }
            });

            _this.entity.on("mouseIn", function (event) {
                if (!_this.isActive && !_this.isHover) {
                    _this.entity.polygon.material.color = new uMap.Color(_this.colors.hover);
                    _this.isHover = true;
                }
            });

            _this.entity.on("mouseOut", function (event) {
                if (!_this.isActive) {
                    _this.entity.polygon.material.color = new uMap.Color(_this.colors.normal);
                }
                _this.isHover = false;
            });

            _this.entity.on("click", function (event) {
                _this.trigger("click", event);
            });

            _this.layer.dataSources.entities.add(_this.entity.object);

        }


    }

    var _class = RoadMaterials;

    mhjs.OptionToPropertiesInterface(_class);
    mhjs.EventInterface(_class);

    Object.defineProperties(_class.prototype, {
        "open"  : {
            value: function () {
                if (!this.isOpened) {
                    this.layer && this.layer.open();
                    this.isOpened = true;
                    this.trigger("open");
                }
            }
        },
        "close" : {
            value: function () {
                if (this.isOpened) {
                    this.layer && this.layer.close();
                    this.isOpened = false;
                    this.trigger("close");
                }
            }
        },
        "toggle": {
            value: function () {
                if (this.isOpened) {
                    this.close();
                } else {
                    this.open();
                }
            }
        },
        "active": {
            value: function (isActive) {
                var _this = this;

                isActive = (isActive === undefined) || Boolean(isActive);
                _this.isActive = _this.isActive === undefined ? false : _this.isActive;

                if (isActive !== _this.isActive) {

                    if (isActive) {

                        if (_this.layer) {
                            _this.entity.polygon.material.color = new uMap.Color(_this.colors.active);
                        }
                        _this.trigger("active");
                    } else {

                        if (_this.layer) {
                            _this.entity.polygon.material.color = new uMap.Color(_this.colors.normal);
                        }
                        _this.trigger("unActive");
                    }

                    _this.isActive = isActive;

                }

            }
        }
    });

    return _class;

});